﻿var map;
var geocoder;
var baseIcon;

function getMap(div_id, property_lat, property_lng, property_info, comp_address, comp_info) 
{
    map = new GMap2(document.getElementById(div_id));
    map.addControl(new GSmallZoomControl3D());
    map.setMapType(G_HYBRID_MAP);
    var property_loc = new GLatLng(property_lat, property_lng);
    var zoom = 15;
    map.setCenter(property_loc, zoom);
 
    var property_icon = new GIcon(G_DEFAULT_ICON);
    property_icon.image = "../images/BlueGMarker.png";
    var property_marker = new GMarker(property_loc, { icon: property_icon });
    GEvent.addListener(property_marker, "click", function() {
    property_marker.openInfoWindowHtml(property_info);
    }); 
    map.addOverlay(property_marker);   
  
    geocoder = new GClientGeocoder();
    geocoder.setBaseCountryCode("us");
    
    baseIcon = new GIcon(G_DEFAULT_ICON);

    if (geocoder && comp_address != null) {

        if (comp_info == null || comp_info.length < comp_address.length) {
            comp_info = comp_address; 
        }
    
        for (var i = 0; i < comp_address.length; i++) {       
            callGetLatLng(comp_address[i], comp_info[i], i);   
        } 
    }
}

function callGetLatLng(address, info, index) {
    geocoder.getLatLng(address, function(point) {
    if (!point) {
    }
    else {
        map.addOverlay(createMarker(point, info, index));
    }
    });
} 

function createMarker(point, info, index) {
    // Create a lettered icon for this point using our icon class
    var letter = String.fromCharCode("A".charCodeAt(0) + index);
    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

    // Set up our GMarkerOptions object
    markerOptions = { icon: letteredIcon };
    var marker = new GMarker(point, markerOptions);

    GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(info);
    });
    return marker;
}